<template>
    <div class="app-container">

        <el-form label-width="120px">

            <el-form-item label="课程标题">
                <el-input v-model="courseInfo.title" placeholder=" 示例：机器学习项目课：从基础到搭建项目视频课程。专业名称注意大小写" />
            </el-form-item>

            <!-- 所属分类  -->
            <el-form-item label="课程分类">
                <el-select v-model="courseInfo.subjectParentId" placeholder="请选择" @change="subjectChanged">
                    <el-option v-for="subject in subjectList" :key="subject.id" :label="subject.title"
                        :value="subject.id"></el-option>
                </el-select>

                <el-select v-model="courseInfo.subjectId" placeholder="请选择">
                    <el-option v-for="subject in subjectChildrenList" :key="subject.id" :label="subject.title"
                        :value="subject.id"></el-option>
                </el-select>
            </el-form-item>

            <!-- 课程讲师  -->
            <el-form-item label="课程讲师">
                <el-select v-model="courseInfo.teacherId">
                    <el-option v-for="teacher in teacherList" :key="teacher.id" :label="teacher.name"
                        :value="teacher.id"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="总课时">
                <el-input-number :min="0" v-model="courseInfo.lessonNum" controls-position="right"
                    placeholder="请填写课程的总课时数" />
            </el-form-item>


            <!-- 课程简介  -->
            <el-form-item label="课程简介">
                <editor :height="300" v-model="courseInfo.description" />
            </el-form-item>

            <!-- 课程封面-->
            <el-form-item label="课程封面">
                <el-upload :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                    action="http://localhost:8120/admin/oss/file/upload?module=cover" class="cover-uploader">
                    <img v-if="courseInfo.cover" :src="courseInfo.cover">
                    <i v-else class="el-icon-plus avater-uploader-icon" />
                </el-upload>
            </el-form-item>

            <el-form-item label="课程价格">
                <el-input-number :min="0" v-model="courseInfo.price" controls-position="right" placeholder="免费课程请设置为0元" /> 元
            </el-form-item>
        </el-form>

        <div style="text-align: center;">
            <el-button :disabled="saveBtnDisabled" type="primary" @click="saveAndNext">
                保存并下一步
            </el-button>
        </div>
    </div>
</template>

<script>
import courseApi from '@/api/course'
import teacherApi from "@/api/teacher";
import subjectApi from '@/api/subject'

import Editor from '@tinymce/tinymce-vue'

export default {
    components: { Editor },
    data() {
        return {
            saveBtnDisabled: false,  // 按钮是否禁用
            courseInfo: {  // 课程信息
                title: '',
                subjectId: '',
                teacherId: '',
                lessonNum: 0,
                description: '',
                cover: '',
                price: 0
            },
            teacherList: [], // 讲师列表
            subjectList: [], // 课程分类一级列表
            subjectChildrenList: [],// 课程分类二级列表
        }
    },
    created() {
        // courseId 来源自路由
        // if (this.$route.name === 'CourseInfoEdit') {
        //     this.$parent.courseId = this.$route.params.id
        // }

        // courseId 来源自父组件
        // 回显
        if (this.$parent.courseId) {
            this.fetchCourseInfoById(this.$parent.courseId)
        } else { // 新增只渲染一级类别
            this.initSubjectList()
        }
        this.initTeacherList()

    },
    methods: {

        fetchCourseInfoById(id) {
            courseApi.getCourseInfoById(id).then(response => {
                this.courseInfo = response.data.item

                // 课程类别渲染
                subjectApi.getNestedTreeList().then(response => {
                    this.subjectList = response.data.items
                    this.subjectList.forEach(subject => {
                        if (subject.id === this.courseInfo.subjectParentId) {
                            this.subjectChildrenList = subject.children
                        }
                    })
                })
            })
        },

        // 获取讲师列表
        initTeacherList() {
            teacherApi.list().then(response => {
                this.teacherList = response.data.items
            })
        },
        // 获取课程分类列表
        initSubjectList() {
            subjectApi.getNestedTreeList().then(response => {
                this.subjectList = response.data.items
            })
        },
        // 保存并下一步
        saveAndNext() {
            this.saveBtnDisabled = true
            if (this.$parent.courseId) {
                this.updateData()
            } else {
                this.saveData()
            }

        },
        saveData() {
            courseApi.saveCourseInfo(this.courseInfo).then(response => {
                this.$message.success(response.message)
                this.$parent.courseId = response.data.courseId
                this.$parent.active = 2
            })
        },
        updateData() {
            courseApi.updateCourseInfo(this.courseInfo).then(response => {
                this.$message.success(response.message)
                this.$parent.active = 2
            })
        },
        // 切换一级类别下拉列表
        subjectChanged(value) {
            this.subjectList.forEach(subject => {
                if (subject.id === value) {
                    this.courseInfo.subjectId = ''
                    this.subjectChildrenList = subject.children
                }
            })
        },

        // 上传成功的回调
        handleAvatarSuccess(res, file) {
            if (res.success) {
                this.courseInfo.cover = res.data.url
                this.$forceUpdate
            } else {
                this.$message.error('上传失败')
            }
        },
        // 上传校验
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'
            const isLt2M = file.size / 1024 / 1024 < 2
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!')
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        }
    }
}
</script>

<style scoped>
.cover-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.cover-uploader .el-upload:hover {
    border-color: #409eff;
}

.cover-uploader .avater-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 640px;
    height: 357px;
    line-height: 357px;
    text-align: center;
}

.cover-uploader img {
    width: 640px;
    height: 357px;
    display: block;
}
</style>